package com.customer.controller.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.customer.entity.AIMessage;
import com.customer.entity.AISession;
import com.customer.result.Result;
import com.customer.service.AISessionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/user/ai-session")
@Api(tags = "AI会话相关接口")
@Slf4j
public class AISessionController {
    
    @Autowired
    private AISessionService aiSessionService;
    
    /**
     * 创建新会话
     */
    @PostMapping("/create")
    @ApiOperation("创建新AI会话")
    // 修复：将参数名从character改为role，与服务接口保持一致
    public Result<AISession> createSession(@RequestParam Long userId, 
                                         @RequestParam String role, 
                                         @RequestParam(required = false) String initialMessage) {
        log.info("创建新AI会话 - userId: {}, role: {}, initialMessage: {}", userId, role, initialMessage);
        AISession session = aiSessionService.createSession(userId, role, initialMessage);
        return Result.success(session);
    }
    
    /**
     * 获取用户的会话列表
     */
    @GetMapping("/list")
    @ApiOperation("获取用户的AI会话列表")
    public Result<List<AISession>> getSessionList(@RequestParam Long userId, 
                                               @RequestParam(required = false) String role) {
        log.info("获取用户的AI会话列表 - userId: {}, role: {}", userId, role);
        List<AISession> sessions;
        if (role != null && !role.isEmpty()) {
            // 如果提供了角色参数，则按用户ID和角色查询
            sessions = aiSessionService.getUserSessionsByRole(userId, role);
        } else {
            // 否则查询所有会话
            sessions = aiSessionService.getUserSessions(userId);
        }
        return Result.success(sessions);
    }
    
    /**
     * 获取会话详情
     */
    @GetMapping("/detail")
    @ApiOperation("获取AI会话详情")
    public Result<AISession> getSessionDetail(@RequestParam String sessionId) {
        log.info("获取AI会话详情 - sessionId: {}", sessionId);
        AISession session = aiSessionService.getSessionBySessionId(sessionId);
        return Result.success(session);
    }
    
    /**
     * 获取会话的消息列表
     */
    @GetMapping("/messages")
    @ApiOperation("获取AI会话的消息列表")
    public Result<List<AIMessage>> getSessionMessages(@RequestParam Long sessionId) {
        log.info("获取AI会话的消息列表 - sessionId: {}", sessionId);
        List<AIMessage> messages = aiSessionService.getSessionMessages(sessionId);
        return Result.success(messages);
    }
    
    /**
     * 删除会话
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除AI会话")
    public Result<String> deleteSession(@PathVariable Long id) {
        log.info("删除AI会话 - id: {}", id);
        aiSessionService.deleteSession(id);
        return Result.success();
    }
    
    /**
     * 更新会话标题
     */
    @PutMapping("/title")
    @ApiOperation("更新AI会话标题")
    public Result<String> updateSessionTitle(@RequestParam String sessionId, @RequestParam String title) {
        log.info("更新AI会话标题 - id: {}, title: {}", sessionId, title);
        aiSessionService.updateSessionTitle(sessionId, title);
        return Result.success();
    }
}